{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 我的第一个智能机器人——报时机器人\n",
    "\n",
    "智能机器人是一个很好玩的大玩具，可以与他问问题、聊天等。是不是很期待啊，本节我们最一个简单的智能机器人——报时机器人。\n",
    "\n",
    "我们问几点了，机器人就会告送我们当前时间。\n",
    "\n",
    "本节我们利用语音识别、语音合成和我们的简单控制逻辑，做一个报时机器人。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下会报时的小派,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "from time import strftime,gmtime\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_speech_recognition import baidu_speech_2_word\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "import voice_record\n",
    "\n",
    "\"\"\" 公开课的有限量的语音识别，必须更换为自己的api接口　\"\"\"\n",
    "SP_APP_ID = '20672409 '\n",
    "SP_API_KEY = 'rFCk7LeFz9YcYbu0ZD6QL10G'\n",
    "SP_SECRET_KEY = 'rRHHmclgM3TGzCIXV3103XvvXOasjMX1'\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数给语音识别模块\n",
    "        sp_2_wd = baidu_speech_2_word(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        #传入百度AI的参数给语音合成模块\n",
    "        word_2_sound = baidu_word_2_sound(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        while True:\n",
    "            #进行麦克风录音，每次录音6秒\n",
    "            voice_record.recording(\"repeater_raw.wav\", 6)\n",
    "\n",
    "            #将音频采样率更改为百度需要的16000\n",
    "            os.system('sox repeater_raw.wav -r 16000 repeater.wav')\n",
    "            \n",
    "            #对音频文件进行分析\n",
    "            msg = sp_2_wd.speech_2_word('repeater.wav')\n",
    "\n",
    "            #语音识别正确，则进行语音合成和输出\n",
    "            if msg[\"err_no\"] == 0:\n",
    "                print(msg[\"result\"][0])\n",
    "                if msg[\"result\"][0][0:4]== \"小派小派\":\n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound(\"小派在呢。\",'repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.wav')\n",
    "                \n",
    "                elif msg[\"result\"][0][0:4] == \"现在几点\":\n",
    "                    #获取时间\n",
    "                    now_time_data = time.strftime(\"时间为，%Y年%m月%d日,%H时%M分%S秒,%A,今天是今年的第%j天\")\n",
    "                    print(now_time_data)\n",
    "                    \n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound(now_time_data,'repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.wav')\n",
    "                    \n",
    "                else:\n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound(\"小派没听清楚,请说,现在几点了\",'repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.wav')\n",
    "            #延时\n",
    "            time.sleep(1)\n",
    "        \n",
    "        #删除临时文件\n",
    "        os.remove('repeater.wav')\n",
    "        os.remove('repeater_raw.wav')\n",
    "        \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('repeater.wav')\n",
    "        os.remove('repeater_raw.wav')\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，制作智能的小派"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #导入标准库\n",
    "# import sys\n",
    "\n",
    "# import os\n",
    "\n",
    "# import time\n",
    "\n",
    "# from time import strftime,gmtime\n",
    "\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "\n",
    "# from baidu_speech_recognition import baidu_speech_2_word\n",
    "\n",
    "# from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "# import voice_record\n",
    "\n",
    "# \"\"\" 公开课的有限量的语音识别，必须更换为自己的api接口　\"\"\"\n",
    "# SP_APP_ID = '20672409 '\n",
    "\n",
    "# SP_API_KEY = 'rFCk7LeFz9YcYbu0ZD6QL10G'\n",
    "\n",
    "# SP_SECRET_KEY = 'rRHHmclgM3TGzCIXV3103XvvXOasjMX1'\n",
    "\n",
    "\n",
    "# if __name__ == \"__main__\":\n",
    "\n",
    "#     try:\n",
    "\n",
    "#         #传入百度AI的参数给语音识别模块\n",
    "#         sp_2_wd = baidu_speech_2_word(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "\n",
    "        \n",
    "#         #传入百度AI的参数给语音合成模块\n",
    "#         word_2_sound = baidu_word_2_sound(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "\n",
    "        \n",
    "#         while True:\n",
    "\n",
    "#             #进行麦克风录音，每次录音6秒\n",
    "#             voice_record.recording(\"repeater_raw.wav\", 6)\n",
    "\n",
    "\n",
    "#             #将音频采样率更改为百度需要的16000\n",
    "#             os.system('sox repeater_raw.wav -r 16000 repeater.wav')\n",
    "            \n",
    "#             #对音频文件进行分析\n",
    "#             msg = sp_2_wd.speech_2_word('repeater.wav')\n",
    "\n",
    "\n",
    "#             #语音识别正确，则进行语音合成和输出\n",
    "#             if msg[\"err_no\"] == 0:\n",
    "\n",
    "#                 print(msg[\"result\"][0])\n",
    "\n",
    "#                 if msg[\"result\"][0][0:4]== \"小派小派\":\n",
    "\n",
    "#                     #进行语音合成\n",
    "#                     word_2_sound.trans_word_to_sound(\"小派在呢。\",'repeater.wav')\n",
    "\n",
    "#                     os.system('mplayer ' + 'repeater.wav')\n",
    "\n",
    "                \n",
    "#                 elif msg[\"result\"][0][0:4] == \"现在几点\":\n",
    "\n",
    "#                     #获取时间\n",
    "#                     now_time_data = time.strftime(\"时间为，%Y年%m月%d日,%H时%M分%S秒,%A,今天是今年的第%j天\")\n",
    "\n",
    "#                     print(now_time_data)\n",
    "\n",
    "                    \n",
    "#                     #进行语音合成\n",
    "#                     word_2_sound.trans_word_to_sound(now_time_data,'repeater.wav')\n",
    "\n",
    "#                     os.system('mplayer ' + 'repeater.wav')\n",
    "\n",
    "                    \n",
    "#                 else:\n",
    "\n",
    "#                     #进行语音合成\n",
    "#                     word_2_sound.trans_word_to_sound(\"小派没听清楚,请说,现在几点了\",'repeater.wav')\n",
    "\n",
    "#                     os.system('mplayer ' + 'repeater.wav')\n",
    "\n",
    "#             #延时\n",
    "#             time.sleep(1)\n",
    "\n",
    "        \n",
    "#         #删除临时文件\n",
    "#         os.remove('repeater.wav')\n",
    "\n",
    "#         os.remove('repeater_raw.wav')\n",
    "\n",
    "        \n",
    "#     except KeyboardInterrupt:\n",
    "\n",
    "#         os.remove('repeater.wav')\n",
    "\n",
    "#         os.remove('repeater_raw.wav')\n",
    "\n",
    "#         print(\"任务被终止了\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "\n",
    "在if语句中，添加自己的想法和控制逻辑，实现更智能的小派，例如询问天气预报、报告空气指数等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下会报时的小派,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "from time import strftime,gmtime\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_speech_recognition import baidu_speech_2_word\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "import voice_record\n",
    "\n",
    "\"\"\" 公开课的有限量的语音识别，必须更换为自己的api接口　\"\"\"\n",
    "SP_APP_ID = '20672409 '\n",
    "SP_API_KEY = 'rFCk7LeFz9YcYbu0ZD6QL10G'\n",
    "SP_SECRET_KEY = 'rRHHmclgM3TGzCIXV3103XvvXOasjMX1'\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #传入百度AI的参数给语音识别模块\n",
    "        sp_2_wd = baidu_speech_2_word(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        #传入百度AI的参数给语音合成模块\n",
    "        word_2_sound = baidu_word_2_sound(SP_APP_ID, SP_API_KEY, SP_SECRET_KEY)\n",
    "        \n",
    "        while True:\n",
    "            #进行麦克风录音，每次录音6秒\n",
    "            voice_record.recording(\"repeater_raw.wav\", 6)\n",
    "\n",
    "            #将音频采样率更改为百度需要的16000\n",
    "            os.system('sox repeater_raw.wav -r 16000 repeater.wav')\n",
    "            \n",
    "            #对音频文件进行分析\n",
    "            msg = sp_2_wd.speech_2_word('repeater.wav')\n",
    "\n",
    "            #语音识别正确，则进行语音合成和输出\n",
    "            if msg[\"err_no\"] == 0:\n",
    "                print( msg[\"result\"][0])\n",
    "                if msg[\"result\"][0][0:2]== \"小派\":\n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound(\"小派在呢。\",'repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.wav')\n",
    "                \n",
    "                elif msg[\"result\"][0][0:5] == \"现在几点了\":\n",
    "                    #获取时间\n",
    "                    now_time_data = time.strftime(\"时间为，%Y年%m月%d日,%H时%M分%S秒,%A,今天是今年的第%j天\")\n",
    "                    \n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound(now_time_data,'repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.mp3')\n",
    "                    \n",
    "                elif msg[\"result\"] == \"天气预报\":\n",
    "                    #添加自己的天气预报代码\n",
    "                    \n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound('自己的天气预报','repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.wav')\n",
    "                    \n",
    "                else:\n",
    "                    #进行语音合成\n",
    "                    word_2_sound.trans_word_to_sound(\"小派没听清楚,请说,现在几点了\",'repeater.wav')\n",
    "                    os.system('mplayer ' + 'repeater.wav')\n",
    "            #延时\n",
    "            time.sleep(1)\n",
    "        \n",
    "        #删除临时文件\n",
    "        os.remove('repeater.wav')\n",
    "        os.remove('repeater_raw.wav')\n",
    "        \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('repeater.wav')\n",
    "        os.remove('repeater_raw.wav')\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
